package com.kfm.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.concurrent.atomic.AtomicInteger;


@WebListener
public class OnlineUserListener implements HttpSessionListener {

    private AtomicInteger count = new AtomicInteger(0);
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("创建了一个session：" + session.getId());
        System.out.println("现有 " + count.incrementAndGet() + " 用户在线");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("销毁了一个session：" + session.getId());
        System.out.println("现有 " + count.decrementAndGet() + " 用户在线");
    }
}
